package io.sage.modules.outwarehouse.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.sage.common.utils.R;
import io.sage.modules.outwarehouse.entity.BEmployeeAccessRecord;
import io.sage.modules.outwarehouse.service.BEmployeeAccessRecordService;



/**
 * 员工出入记录表
 *
 * @author xujh
 * @email jhang_mail@163.com
 * @date 2019-07-09 20:15:37
 */
@RestController
@RequestMapping("outwarehouse/bemployeaccessrecord")
public class BEmployeaccessRecordController {
    @Autowired
    private BEmployeeAccessRecordService employeaccessRecordService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("outwarehouse:bemployeaccessrecord:list")
    public R list(@RequestParam Map<String, Object> params){
    	Integer pageSize = 1;
		if (params != null && params.get("limit") != null) {
			pageSize = java.lang.Integer.parseInt(params.get("limit").toString());
		}
		Integer pageNum = 10;
		if (params != null && params.get("page") != null) {
			pageNum = java.lang.Integer.parseInt(params.get("page").toString());
		}

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		if(params.get("examinationName")!=null){

			if(!"".equals(params.get("examinationName").toString())){
				paramMap.put("examinationName", params.get("examinationName").toString());
			}
		}

		if(params.get("studentName")!=null){
			if(!"".equals(params.get("studentName").toString())){
				paramMap.put("studentName", params.get("studentName").toString());
			}
		}

		PageHelper.startPage(pageNum, pageSize);// 设置分页的起始码以及页面大小
		List<BEmployeeAccessRecord> selectEmployeeAccessRecordList = employeaccessRecordService.selectEmployeeAccessRecordList(paramMap);

		PageInfo<BEmployeeAccessRecord> pageInfo = new PageInfo<BEmployeeAccessRecord>(selectEmployeeAccessRecordList);// 传入list就可以了

		return R.ok().put("employeeAccessRecordList", pageInfo);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("outwarehouse:bemployeaccessrecord:info")
    public R info(@PathVariable("id") Integer id){
    	BEmployeeAccessRecord employeeAccessRecord = employeaccessRecordService.selectByPrimaryKey(id);
		Map<String, Object> paramMap = new HashMap<String, Object>();

		paramMap.put("employeeAccessRecord", employeeAccessRecord);

		return R.ok().put("map", paramMap);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("outwarehouse:bemployeaccessrecord:update")
    public R update(@RequestBody JSONObject jsonObject){
    	BEmployeeAccessRecord employeeAccessRecord = jsonObject.getObject("employeeAccessRecord", BEmployeeAccessRecord.class);
		
		if(employeeAccessRecord.getId()==null){
			return R.error("需要修改的id，不能为空");
		}
		int update = employeaccessRecordService.updateByPrimaryKeySelective(employeeAccessRecord);//全部更新

		if(update>0){
			return R.ok();
		}
		return R.error();
	}

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("outwarehouse:bemployeaccessrecord:delete")
    public R delete(@RequestBody Integer[] ids){

		int delete = employeaccessRecordService.deleteBatchIds(ids);

		if(delete>0){
			return R.ok();
		}

		return R.error();
	}

}
